<?php
namespace app\account\controller;

use app\common\model\Base;

class Login extends Base
{
    public function __construct()
    {
        $_SERVER["HTTP_X_TOKEN"] = "none";
        parent::__construct();
    }

    /**
     *
     * @api {get} /account/Login  登录
     * @apiDescription 用户登录
     * @apiGroup account
     * @apiVersion 0.1.0
     * @apiParam {string} tel 电话号码
     * @apiParam {string} passd 密码
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "tel": 17754928306，
     *       "passd": 123456789
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 登录的个人信息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */
    public function index()
    {
        $tel = $_GET["tel"];
        $passd = $_GET["passd"];

        if(!$this->check->isLoginNull($tel,$passd))
            exit();

        $this->selectUserInfo($tel,$passd);
    }

    

    public function selectUserInfo($tel, $passd)
    {
        $sql = "SELECT A.tel,A.uid,A.passd,A.nickname,A.headpic,A.balance,A.bonus_point,A.alipay_name,A.alipay_account,A.agency_grade_id,"
            . "B.grade_name,grade_discounts FROM `user` AS A LEFT JOIN agency_shop_grade AS B "
            . "ON A.agency_grade_id=B.grade_id WHERE A.tel='$tel'";
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {
            if ($row["passd"] !== $passd) {//账号不存在
                echo echoJson(0, "账号或密码错误!");
                return;
            }

            $nickname = !empty($row['nickname']) ? $row['nickname'] : $row['uid'];
            $headpic = !empty($row['headpic']) ? $row['headpic'] : "";
            $balance = !empty($row['balance']) ? $row['balance'] : 0.0;
            $integral = !empty($row['bonus_point']) ? $row['bonus_point'] : 0.0;
            $agency_grade_id = !empty($row['agency_grade_id']) ? $row['agency_grade_id'] : 0;
            $grade_name = !empty($row['grade_name']) ? $row['grade_name'] : "未知";
            $grade_discounts = !empty($row['grade_discounts']) ? $row['grade_discounts'] : 1;
            $alipay_name = !empty($row['alipay_name'])?$row['alipay_name']:"";
            $alipay_account = !empty($row['alipay_account'])?$row['alipay_account']:"";
            echo echoJson(1, array("token"=>$this->token($row['uid'],$nickname),"uid" => $row["uid"], "nickname" => $nickname, "headpic" => $headpic, "balance" => $balance,
                "integral" => $integral, "agency_grade_id" => $agency_grade_id, "grade_name" => $grade_name,
                "grade_discounts" => $grade_discounts,"alipay_name"=>$alipay_name,"alipay_account"=>$alipay_account, c));
        } else {
            echo echoJson(0, "账号不存在!");
        }
    }
}
